<?php

namespace app\api\controller;

use app\api\Request;
use app\BaseController;
use think\response\Json;

class Base extends BaseController
{
    /**
     * user_id
     * @var int
     */
    protected $userId = 0;

    // init
    protected function initialize()
    {
        $this->setRequest(app(Request::class));
        $this->initUserIdToRequest();
    }

    /**
     * addUserIdToRequest
     */
    protected function initUserIdToRequest()
    {
        $middlewareKey = 'userId';
        $request = app(\think\Request::class);
        if ($request->$middlewareKey)
        {
            $this->request->setParam($middlewareKey, $request->$middlewareKey);
        }
    }

    /**
     * response
     * @param array $data
     * @param int $code
     * @param string $info
     * @return Json
     */
    public function response(array $data = [], int $code = 1, string $info = 'success'): Json
    {
        $response = [
            'code' => $code,
            'data' => $data,
            'message' => $info,
        ];
        return json($response);
    }
}
